<template>
  <div class="content" v-loading="submitBtn">
    <div class="scroll-view-box">
      <el-form label-width="120px" :inline="true" :model="formData" :rules="formRules" ref="elFrom"
        label-position="top">
        <el-form-item label="项目名称" prop="project_id">
          <el-select v-model="formData.project_id" placeholder="请选择项目名称" :disabled="editType == 'edit'" filterable
            clearable class="input-width">
            <el-option v-for="(item, index) in projectList" :key="index" :label="item.project_name"
              :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="工程监理" prop="jl_idx" v-if="userInfo.position_id != 8">
          <el-select v-model="formData.jl_idx" placeholder="请选择工程监理" filterable clearable class="input-width">
            <el-option v-for="(item, idx) in jlList" :key="idx" :label="item.name" :value="idx">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="工程经理" prop="gc_idx" v-if="userInfo.position_id == 8">
          <el-select v-model="formData.gc_idx" placeholder="请选择工程经理" filterable clearable class="input-width">
            <el-option v-for="(item, idx) in gcList" :key="idx" :label="item.name" :value="idx">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="售后内容" prop="problem_content">
          <el-input type="textarea" v-model="formData.problem_content" placeholder="请输入售后内容" clearable
            class="input-width"></el-input>
        </el-form-item>
        <el-form-item label="现场对接联系电话" prop="scene_phone">
          <el-input v-model="formData.scene_phone" placeholder="请输入现场对接联系电话" :disabled="editType == 'edit'" clearable
            v-input-limit="0" class="input-width"></el-input>
        </el-form-item>
        <el-form-item label="期望上门时间" prop="expected_doorstep_time">
          <el-date-picker v-model="formData.expected_doorstep_time" :disabled="editType == 'edit'" type="date"
            value-format="yyyy-MM-dd" placeholder="请选择期望上门时间" class="input-width" :picker-options="pickerOptionsEnd">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="上传图片/视频" prop="after_sales_file">
          <div class="file-list-box">
            <div class="file-item-box" v-for="(item, index) in formData.after_sales_file" :key="index">
              <div class="el-icon-delete-box">
                <i class="el-icon-error" @click="delFileFunc(index)"></i>
              </div>
              <el-tooltip class="item" effect="dark" :content="item.id ? item.file_name : item.name" placement="top">
                <img class="img" :src="$fileImgFunc(item)" />
              </el-tooltip>
            </div>
            <el-upload multiple class="avatar-uploader" action="#" accept="image/*,video/*,.m4a,.mp3"
              :show-file-list="false" :auto-upload="false" list-type="picture-card" :on-change="changeFile">
              <i class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="saveSubmit" :loading="submitBtn">提交</el-button>
        <el-button @click="cancelFunc">关闭</el-button>
      </div>
    </div>
  </div>
</template>
<script>
import asApi from '@/api/afterSalesApi'
import adminApi from '@/api/adminApi';
import { getUserInfo } from '@/utils/auth';

export default {
  name: 'afterSalesManagementAdd',
  data() {
    let validateFileList = (rule, value, callback) => {
      console.log(rule, value)
      if (!value.length) {
        callback(new Error('请选择附件'));
      } else {
        callback();
      }
    };
    return {
      userInfo: {},
      projectList: [],
      jlList: [],
      gcList: [],

      pickerOptionsEnd: {
        disabledDate(time) {
          return time.getTime() < Date.now() - 8.64e7;
        },
      },
      loading: false,
      editType: 'add',
      delFile: [],
      formData: {
        id: '',
        project_id: '',
        jl_idx: '',
        gc_idx: '',
        expected_doorstep_time: '',
        scene_phone: '',
        problem_content: '',
        after_sales_file: [],
      },
      formRules: {
        project_id: { required: true, message: "请选择项目名称", trigger: "change" },
        jl_idx: { required: true, message: "请选择工程监理", trigger: "change" },
        gc_idx: { required: true, message: "请选择工程经理", trigger: "change" },
        problem_content: { required: true, message: "请输入售后内容", trigger: "change" },
        scene_phone: { required: true, message: "请输入现场对接联系电话", trigger: "change" },
        expected_doorstep_time: { required: true, message: "请选择期望上门时间", trigger: "change" },
        after_sales_file: { required: true, validator: validateFileList, trigger: "change" },
      },
      submitBtn: false,
    };
  },
  computed: {
  },
  mounted() {
    this.userInfo = getUserInfo();

    let id = this.$route.query.sn
    if (id) {
      asApi.newAfterSalesDetails({ after_sales_id: id }).then(res => {
        console.log(res, '售后详情')
        if (res.code == 200) {
          this.asInfo = res.data;
          this.editType = 'edit'
          this.formData.id = id
          this.formData.project_id = res.data.project_id
          this.formData.problem_content = res.data.problem_content
          this.formData.after_sales_file = res.data.after_sales_attachments
          this.formData.expected_doorstep_time = res.data.expected_doorstep_time
          this.formData.scene_phone = res.data.scene_phone
        }
      })
    }

    this.getList()
  },
  methods: {
    // 选择文件
    changeFile(file) {
      this.formData.after_sales_file.push(file)
    },
    delFileFunc(index) {
      if (this.formData.after_sales_file[index].id) {
        this.delFile.push(this.formData.after_sales_file[index].id);
      }
      this.$nextTick(() => {
        this.formData.after_sales_file.splice(index, 1);
      })
    },
    // 提交
    saveSubmit() {
      this.$refs['elFrom'].validate((valid) => {
        if (valid) {
          let formData = this.formData
          let userInfo = this.userInfo
          let formdata = new FormData();
          let data = {
            project_id: formData.project_id,
            expected_doorstep_time: formData.expected_doorstep_time,
            scene_phone: formData.scene_phone,
            problem_content: formData.problem_content,
          };
          if (userInfo.position_id == 8) {
            data.engineering_supervision_id = userInfo.userid
            data.engineering_supervision_name = userInfo.nickName
            data.engineering_manager_id = this.gcList[formData.gc_idx].id
            data.engineering_manager_name = this.gcList[formData.gc_idx].name
          } else {
            data.engineering_supervision_id = this.jlList[formData.jl_idx].id
            data.engineering_supervision_name = this.jlList[formData.jl_idx].name
          }
          if (this.editType == 'edit') {
            data.after_sales_id = formData.id
            data.del_file_id_arr = this.delFile
          }
          formdata.append("data", JSON.stringify(data));
          formData.after_sales_file.map(item => {
            if (item.raw) {
              formdata.append("after_sales_file[]", item.raw);
            }
          })

          this.submitBtn = true;
          let server
          if (this.editType == 'edit') {
            server = asApi.updAfterSales(formdata)
          } else {
            server = asApi.addAfterSales(formdata)
          }
          server.then(res => {
            console.log(res, '发起售后')
            if (res.code == 200) {
              this.$message({
                message: "发起成功",
                type: "success",
              });
              this.cancelFunc()
            } else {
              this.$message.error(res.message);
            }
            this.submitBtn = false;
          })
        } else {
          return false;
        }
      });
    },
    // 取消
    cancelFunc() {
      this.$goBackTab(this);
    },

    /****************获取关联数据*********************/
    getList() {
      //获取项目列表
      asApi.getAfterSalesProjects({
      }).then(res => {
        console.log(res, "获取项目列表")
        if (res.code == 200) {
          this.projectList = res.data;
        }
      })
      if (this.userInfo.position_id == 8) {
        // 获取工程经理列表
        adminApi.getAdminList({
          page: 1,
          num: 999,
          position_id_arr: [7],
          identity: 2,
        }).then((res) => {
          console.log(res, "获取工程经理列表");
          if (res.code == 200) {
            this.gcList = res.data.list;
          }
        })
      } else {
        // 获取工程监理列表
        adminApi.getAdminList({
          page: 1,
          num: 999,
          position_id_arr: [8],
          identity: 1,
        }).then((res) => {
          console.log(res, "获取工程监理列表");
          if (res.code == 200) {
            this.jlList = res.data.list;
          }
        })
      }
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/styles/addPage.scss";
</style>